import { getEnvVersion, getToken} from './auth.js';

let version = getEnvVersion();
 
let baseApiURL = 'http://192.168.0.110:9999';
if(version == 'develop'){
	baseApiURL = "http://192.168.0.110:9999";
}else if(version == 'trial'){
	baseApiURL = "http://192.168.0.110:9999";
}else if(version == 'release'){
	baseApiURL = "http://192.168.0.110:9999";
}

export default   (config) => {
	let header = {
	    'Content-Type': 'application/json'
	}
	let token =	getToken();
	if(token){
		header['Authorization'] = `Bearer ${getToken()}`;
	}
	let url = config.url;
	let method = config.method || "get";
	let data = config.data || {};
	return new Promise((resolve, reject) => {
	 uni.request({
	   url: baseApiURL + url,
	   method,
	   data,
	   header,
	   timeout:10000,
	   success: (res) => {
		 console.log('响应数据:', res.data)
		 let code = res.data.code;
		 if(code == 200){
			 resolve( res.data) ;
			 return;
		 }else if(code == 401){
			uni.showToast({
				icon:'none',
				title:res.data.msg
			}) 
		 }else{
			uni.showToast({
				icon:'none',
				title:'系统异常，请联系管理员！'
			})  
		 }
		 reject(res.data.msg);
	   },
	   fail: (err) => {
		 console.error('请求失败:', err);
		 reject(err);
	   }
	 });
	})
}